﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Entity;
using System.Data;

namespace Data
{

   public class News_Data
    {
        SqlConnection con;
        SqlCommand comn;
        SqlDataAdapter da;
        DataSet ds;

       

      
        public bool Insert_news(News_Entity news)
        {
            try
            {
                con = Connection.StartConnection();

                comn = new SqlCommand("NewsAdd", con);
                con.Open();
                comn.CommandType = CommandType.StoredProcedure;
                comn.Parameters.Add(new SqlParameter("@topicname", news.Topicname));
                comn.Parameters.Add(new SqlParameter("@topic", news.Topic));
                

                
                comn.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Connection.CloseConnection();
            }

            return true;
        }

        public int Update_news(int newsID, string topicname, string topic)
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("UpdateNews", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@newsID", newsID);
                comn.Parameters.AddWithValue("@topicname", topicname);
                comn.Parameters.AddWithValue("@topic", topic);
                return comn.ExecuteNonQuery();
            }
            catch(Exception e)
            {
                throw new Exception(e.Message) ;
            }
            finally
            {
                Connection.CloseConnection();
            }
            
        }
        public DataTable Load()
        {
            con = Connection.StartConnection();
            comn = new SqlCommand("BindNews", con);
            comn.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comn);

            ds = new DataSet();
            try
            {
                da.Fill(ds, "news");
                return ds.Tables["news"];
            }
            catch
            {
                throw;
            }
            finally
            {
                ds.Dispose();
                da.Dispose();
                con.Close();
                con.Dispose();
            }
        }

        public int Delete(int personID)
        {
            con = Connection.StartConnection();
            con.Open();
            comn = new SqlCommand("DeleteNews", con);
            comn.CommandType = CommandType.StoredProcedure;
            try
            {
                comn.Parameters.AddWithValue("@newsID", personID);
                return comn.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                comn.Dispose();
                con.Close();
                con.Dispose();
            }
        }
    }
}
